package com.showsu.editor.manage;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.showsu.editor.entity.dto.ShuXiuCommonDTO;
import com.showsu.editor.entity.remote.AwemeVideoDetail;
import com.showsu.editor.entity.remote.WhoseCardVideoDetailResp;
import com.showsu.editor.entity.vo.response.DouyinVO;
import com.showsu.editor.entity.vo.response.LiveImgVO;
import com.showsu.editor.enums.ResultCode;
import com.showsu.editor.utils.Constant;
import com.showsu.editor.utils.HttpClientUtil;
import com.showsu.editor.utils.RemoteIntegrationUtil;
import com.showsu.editor.utils.StringUtil;
import com.showsu.editor.utils.aliyun.OssUtil;
import com.showsu.editor.utils.remote.ShuXiuUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author: William
 * @Description:
 * @Date: Created in 1:43 下午 2024/9/14
 * @Modified By:
 */
@Component
@Slf4j
public class WeiboBusinessManager {

    @Autowired
    private ShuXiuUtils shuXiuUtils;

    @Autowired
    private OssUtil ossUtil;





    public LiveImgVO handleLiveImages(String source) {
        LiveImgVO  result=null;
        ShuXiuCommonDTO res=shuXiuUtils.weiboLiveDewater(source);
        if(res==null){
            result=new LiveImgVO();
            result.setRequestSuccess(false);
            return result;
        }
        if(res.getCode().equals(String.valueOf(ResultCode.SUCCESS.getCode()))) {
            if (StringUtils.isNotBlank(res.getData())) {
                try {
                    result = JSON.parseObject(res.getData(), LiveImgVO.class);
                    if(result!=null&&CollectionUtil.isNotEmpty(result.getLiveImages())){
                        result.getLiveImages().forEach(img->{
                            if(StringUtils.isNotBlank(img.getUrl())&&!img.getUrl().contains(Constant.showsuLinkPrefix)){
                                String url=ossUtil.uploadHandleWaterDirFile2AliyunOss(img.getUrl(),
                                        Constant.weiboLiveHandlePrefix + UUID.randomUUID().toString().replaceAll("-","") + ".mp4");
                                img.setUrl(url);
                            }
                            if(StringUtils.isNotBlank(img.getCover())&&!img.getCover().contains(Constant.showsuLinkPrefix)){
                                String url=ossUtil.uploadHandleWaterDirFile2AliyunOss(img.getUrl(),
                                        Constant.weiboLiveHandlePrefix + UUID.randomUUID().toString().replaceAll("-","") + ".jpg");
                                img.setUrl(url);
                            }
                        });
                    }
                } catch (Exception e) {
                    log.error("XhsBusinessManager.handleLiveImages", e);
                }
            }
        }
        return result;
    }



    /**
     * 通过作品链接获取作品id
     * @param contentUrl
     * @return
     */
    public String getAwemeIdFromSource(String contentUrl) {
        if (StringUtils.isBlank(contentUrl)) {
            return null;
        }
        String awemeId = null;
        try {
            String startCut ="";
            if (contentUrl.contains("https://")) {
                startCut="https://";
            } else if (contentUrl.contains("http://")) {
                startCut="http://";
            }
            //针对以下类型的链接的处理：https://weibo.com/7380261104/5098867676416037
            if (StringUtils.isNotBlank(startCut) && ( contentUrl.contains(Constant.weiboLinkPrefix)||contentUrl.contains(Constant.weiboLinkPrefix))) {
                String url = StringUtil.subStringFromHttpUrl(contentUrl, startCut, "?");
                awemeId= StrUtil.subAfter(url,"/",true);
            }

        } catch (Exception e) {
            log.error("WeiboService.getAwemeIdFromSource(String) error", e);
        }
        return awemeId;
    }
}
